<%
if (session.getAttribute("username") == null) {
	%><jsp:forward page="/DBP/login.jsp" /><%
}
%>
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*, connection.*, bean.Question, response.DeviceInterface"%>
<%

String newPoll = request.getParameter("newPoll");
if(newPoll != null){
	boolean isNew = newPoll.equals("true");
}

String fail = request.getParameter("fail");
boolean failrecieved = false;
if(fail != null){
	failrecieved = true;
}

Question currentQuestion = null;
Boolean editingQuestion = false;
String qID = request.getParameter("qid");
if(qID != null){
	editingQuestion = true;
	currentQuestion = DeviceInterface.getQuestion(Integer.parseInt(qID));
}

Connection conn = new DbConnection().getConn();
String pollName = session.getAttribute("pollName").toString();
String pollid = session.getAttribute("pollid").toString();
String addQuestion = session.getAttribute("addQuestion").toString();

if(pollName != null && pollid != null){
	session.setAttribute("pollid", pollid);
	session.setAttribute("pollname", pollName);
}

if(addQuestion != null){
	editingQuestion = true;
}

int numQuestions = 0;
//Get all questions in this poll:
ArrayList<Question> QuestionSet = new ArrayList<Question>();
String sql = "SELECT QuestionID from Poll_Question WHERE PollID="+pollid;
System.out.println(sql);
try{
	PreparedStatement ps = conn.prepareStatement(sql);
	ResultSet rs = ps.executeQuery();
	System.out.println("here");
	while(rs.next()){
		numQuestions++;
		int quesID = rs.getInt(0);
		
		QuestionSet.add(DeviceInterface.getQuestion(quesID));
	}
}catch(SQLException e){
	System.out.println("Error in addQuestions");
	System.out.println(e.toString());
}


%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Question editor</title>
<link rel="stylesheet" href="/DBP/style/stylesheet.css" type="text/css" />

<script type="text/javascript">

function showMultiChoice(){
	document.getElementById("multiChoiceDiv").style.display = "block";

	document.getElementById("alphaNumericDiv").style.display = "none";
	document.getElementById("numericDiv").style.display = "none";
}
function showNumeric(){
	document.getElementById("numericDiv").style.display = "block";

	document.getElementById("alphaNumericDiv").style.display = "none";
	document.getElementById("multiChoiceDiv").style.display = "none";
}
function showAlphaNumeric(){
	document.getElementById("alphaNumericDiv").style.display = "block";

	document.getElementById("numericDiv").style.display = "none";
	document.getElementById("multiChoiceDiv").style.display = "none";
	
}

function SRshowSingle(){
	document.getElementById("numsingle").style.display = "block";
	document.getElementById("numrange").style.display = "none";
}
function SRshowRange(){
	document.getElementById("numsingle").style.display = "none";
	document.getElementById("numrange").style.display = "block";
}

function submitNumRange(){
	document.getElementById("formError").style.display = "none";
	
	if(document.getElementById("rangemax").value == false || document.getElementById("rangemin").value == false){
		document.getElementById("formError").style.display = "block";
		document.getElementById("formError").innerHTML += "Please complete form before submitting";
		return false;	
	}
		
	var max = document.getElementById("rangemax").value * 1;
	var min = document.getElementById("rangemin").value * 1;
	if(isNaN(min) || isNaN(max)){
		document.getElementById("formError").style.display = "block";
		document.getElementById("formError").innerHTML += "Please complete form before submitting";
		return false;
	}
	if(min > max){
		document.getElementById("formError").style.display = "block";
		document.getElementById("formError").innerHTML += "Minimum must be less than maximum";
		return false;
	}
	return true;
}

function titleChange(){
	document.getElementById("title").innerHTML = document.getElementById("textInput").value;
	return true;
}
</script>

</head>
<body>
<div class="bg">
<div class='content'>
<jsp:include page="/nav.jsp" />
	<div class= "pagetitle">Creation -> <%=pollName %> -> Edit Questions</div><br/>
	<div class="entrytitle">
	Edit questions for <%=pollName %>
	<div class="entry">
	<%if(failrecieved){ if(fail.equals("true")){%><div class="error"> Error! questions not created :( Please try again.</div><br/><%}else if(fail.equals("false")){ %><div class="error">Poll created successfully :) 
		</div><br/><%}}%>
   	
   		
     <form name="questionmgt" action="editQuestions.jsp" method="post">
      <div class="tableborder"><table class="blockTable">
        <thead>
        <tr>
          <th scope="col">Questions</th>
          <th></th>
        </tr>
</thead>
<tbody>
 		<tr>
		  	<td><a href="addQuestions.jsp?addQuestion=true&pollid=<%=pollid%>&pollname=<%=pollName%>"><input class="button" type="submit" value="Add new Question"/></a></td>
        </tr>
		<%for(int i = 0; i < numQuestions; i++){
			Question q = QuestionSet.get(i);%>
        <tr>
		  	<td><a href="addQuestions.jsp?qid=<%=q.getQuestionID()%>&pollid=<%=pollid%>&pollname=<%=pollName%>"><%=q.getText()%></a></td>
        </tr>
        
      <td colspan="7"><div align="center"></div>
         <input type="submit" class="button" name="Select" value="Edit" >
      </td>
      <%} %>
        </tbody>
      </table>
      </div>
    </form>
	<%if(editingQuestion){ %>
	<br/>
	</div>
	</div>
	<div class="entrytitle">
		<div id="title">
			<%if(addQuestion.equals("true")){%>
				New Question
			<%}else{ %>
				<%=currentQuestion.getText()%>
			<%} %>
		</div>
	<div class="entry">
	   		<form name="templateForm" action="addQuestions.jsp?template=">
	    	Select template: 
   			<select name="templateSelect">
   			<% //TODO: spit out the options
   			for(int j = 0; j < 3; j++){
   				%><option>[placeholder]</option>
   			<%} %>
   		</select>
   		</form>
	    	<%if(addQuestion != null){ %>
	    	Text:<input type="text" size="40" id="textInput" onkeyup="titleChange()"/><br/>
	    		<table class="blockTable">
	    		<tbody><tr>
	    		
		    	</tr>
		    	<tr>
		    	<td>
			    	Type: <br/>
			    	<input type="radio" name="questionType" OnClick="javascript:showMultiChoice()" value="multichoice"/>Multi Choice
			    	<input type="radio" name="questionType" OnClick="javascript:showNumeric()" value="numeric"/>Numeric Short Response 
			    	<input type="radio" name="questionType" OnClick="javascript:showAlphaNumeric()"value="alphanumeric"/>AlphaNumeric Short Response
		    	</td>
		    	</tr>
		    	</tbody>
		    	</table>
		    	
			    	<div id="multiChoiceDiv" style="display:none">
				    	<form action="insertQuestion.jsp?type=MC&pollid=<%=pollid %>" method="post">
				    		
				    	</form>
			    	</div>
			    	
			    	<div id="numericDiv" style="display:none">
			    		<strong>Numeric Short Response</strong> <br/>
				    	<input type="radio" name="numAnswerType" onClick="javascript:SRshowSingle()"/> Single value answer <br/>
				    	<input type="radio" name="numAnswerType" value="" onClick="javascript:SRshowRange()"/> Range answer <br/>
				    	
				    	<div id="numsingle" style="display:none">
					    	<form action="insertQuestion.jsp?type=SRsingle&pollid=<%=pollid %>" method="post">
						    
					    		<input type="text" name="numsingle" /><br/>
					    		<input type="submit" value="Add" class="button" onClick=" return submitNumSingle(this)"/>
				    		</form>
				    	</div>
					    	
				    	<div id="numrange" style="display:none">
					    	<form action="insertQuestion.jsp?type=SRrange&pollid=<%=pollid %>" method="post">
					    		Minimum:<input type="text" name="rangemin" id="rangemin"/><br/>
					    		Maximum:<input type="text" name="rangemax" id="rangemax"/><br/>
					    		<input type="submit" value="Add" class="button" onClick="return submitNumRange(this)"/>
				    		</form>
				    	</div>
			    	</div>
			    	
			    	<div id="alphaNumericDiv" style="display:none"	>
				    	<form action="insertQuestion.jsp?type=SRalpha&pollid=<%=pollid %>" method="post">
				    		 
				    	</form>
			    	</div>
			    	<div id="formError" class="error"></div>
		    	</form>
	    	<%}else{ %>
		    	Text: <input type="text" value="<%=currentQuestion.getText()%>"/><br/>
		    	Type: <input type="radio" name="questionType" value="Short Response"/>Short Response 
	    	<%} %>
	    
	    
	    </div>
    <%
    }
    %>
    </div>
    </div>
</div>
</body>
</html>
